<template>
	<view class="login-wrap">
		<view class="top-wrap">
			<u--image src="/static/logo-1.png" width="200" height="60" customStyle="margin:0 auto;"></u--image>
		</view>

		<view class="main-wrap">
			<u--text size="18" type="info" bold margin="20rpx 0" text="短信登录"></u--text>
			<u--form :model="loginForm" :rules="rules" ref="form">
				<u-form-item prop="phonenumber" labelWidth="0" borderBottom>
					<u--input v-model="loginForm.phonenumber" placeholder="请输入手机号码" clearable border="none"
						prefixIcon="account-fill"
						prefixIconStyle="font-size: 44rpx; color: #3c9cff; margin-right: 10rpx"
						:clearable="true"></u--input>
				</u-form-item>
				<u-form-item prop="code" labelWidth="0" borderBottom>
					<u-input placeholder="请输入验证码" v-model="loginForm.code" clearable border="none"
						prefixIcon="integral-fill"
						prefixIconStyle="font-size: 44rpx; color: #3c9cff; margin-right: 10rpx" :clearable="true">
						<template slot="suffix">
							<u-code ref="uCode" @change="codeChange" seconds="60" changeText="X秒重新获取"></u-code>
							<u-button @click="getCode" :text="tips" type="success"
								:disabled="this.loginForm.phonenumber == ''"
								style="border:none;"></u-button>
						</template>
					</u-input>
				</u-form-item>
			</u--form>
			<view style="margin-top: 40rpx;"><u-button type="primary" text="登录" @click="handleLogin()"></u-button>
			</view>

			<view class="register-wrap">
				<view style="margin:4rpx 0 0 20rpx;">
					<u-link href="http://www.hpiot.cn/" color="#3c9cff" text="华普物联"></u-link>
				</view>
				<view class="now-btn">
					<u--text text="账号登录" type="primary" @click="handleAccountLogin"></u--text>
				</view>
			</view>
			<view class="footer-wrap">
				<view class="clause">
					<checkbox-group @change="handleClauseCheckbox">
						<label>
							<checkbox :checked="isClause" style="transform:scale(0.6)" />
						</label>
						<text class="check_text">
							<text>已阅读并同意</text>
							<text class="service" @click="handleService">服务协议</text>
							<text>与</text>
							<text class="privacy" @click="handlePrivacy">隐私政策</text>
						</text>
					</checkbox-group>
				</view>
				<view class="copyright"><text>Copyright © 2022-2023 FastBee All Rights Reserved.</text></view>
			</view>
		</view>
	</view>
</template>

<script>
	import projectConfig from '@/env.config.js';
	import {
		getProfile,
		getSmsCode,
		smsLogin
	} from '@/apis/modules/common.js'

	export default {
		data() {
			return {
				token: '',
				tips: '',
				codeUrl: '',
				isClause: false,
				loginForm: {
					phonenumber: '',
					code: '',
				},
				rules: {
					phonenumber: [{
							required: true,
							message: "手机号码不能为空",
							trigger: "blur"
						},
						{
							pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
							message: "请输入正确的手机号码",
							trigger: "blur"
						}
					],
					code: [{
						type: 'integer',
						required: true,
						message: '验证码不能为空',
						trigger: ['blur', 'change']
					}]
				},
			};
		},
		methods: {
			//获取短信验证码
			getCode() {
				if (this.$refs.uCode.canGetCode) {
				if (this.validatePhoneNumber(this.loginForm.phonenumber)) {
					if (this.loginForm.phonenumber) {
						uni.showLoading({
							title: '正在获取验证码'
						});
						getSmsCode(this.loginForm.phonenumber).then(res => {
							if (res.code == 200) {
								uni.$u.toast('验证码已发送');
								// 通知验证码组件内部开始倒计时
								this.$refs.uCode.start();
							} else {
								uni.$u.toast(res.msg);
							}
						})
						.catch(err => {
							console.log("err catch", err);
						})
					} else {
						uni.$u.toast('请输入手机号码');
					}
				} 
				}else {
					uni.$u.toast('倒计时结束在发送！');
				}
			},
			//登录
			handleLogin() {
				this.$refs.form.validate().then(res => {
					if (!this.isClause) {
						uni.showToast({
							icon: 'none',
							title: '请阅读并勾选协议'
						});
						return;
					}
					const params = {
						phonenumber: this.loginForm.phonenumber,
						code: this.loginForm.code,
					}
					// 调用短信登录
					smsLogin(params).then(async res => {
						if (res.code == 200) {
							// 存储token和账号
							this.saveToken(res.data);
							this.saveAccount();
							// 获取用户信息
							let profile = await this.getProfile();
							uni.$u.vuex('profile', profile);
							// 跳转主页
							uni.switchTab({
								url: '/pages/tabBar/home/index'
							});
						} else {
							if (res.msg) {
								uni.showToast({
									icon: "none",
									title: res.msg,
								})
							}
						}
					})
				}).catch(errors => {
					uni.$u.toast('登录信息不能为空');
				});
			},
			// 定义校验函数
			validatePhoneNumber(phone) {
				// 正则表达式规则：1开头的10位或者13、14、15、16、17、18、19开头的11位数字
				const regExp =
					/^((1[3-9]\d{9})|((\+?\d{2,3}-)?(13\d{9}|14[57]|15[0-35-9]|16[6]|17[0135678]|18[\d]{9}|\d{3}-\d{8})))$/;
				return regExp.test(phone);
			},
			// 账号登录
			handleAccountLogin () {
				uni.$u.route('/pages/login/index');
			},
			// 获取用户信息
			getProfile() {
				return new Promise((resolve, reject) => {
					getProfile().then(res => {
						resolve(res.data);
					}).catch(err => {
						this.$u.toast(err.msg);
					})
				}).catch((e) => {});
			},
			codeChange(text) {
				this.tips = text;
			},
			saveToken(token) {
				// 本地缓存存储token
				uni.setStorageSync('token', token);
				// vuex存储token
				uni.$u.vuex('vuex_token', token);
			},
			saveAccount() {
				// 本地缓存存储
				uni.setStorageSync('phonenumber', this.loginForm.phonenumber);
				
			},
			// 服务协议
			handleService() {
				let title = '服务协议';
				let url = projectConfig.officialWebUrl + 'service-agreement.html';
				uni.navigateTo({
					url: `/pages/common/webview/index?title=${title}&url=${url}`
				});
			},
			// 隐私政策
			handlePrivacy() {
				let title = '隐私政策';
				let url = projectConfig.officialWebUrl + 'privacy-policy.html';
				uni.navigateTo({
					url: `/pages/common/webview/index?title=${title}&url=${url}`
				});
			},
			// 勾选协议
			handleClauseCheckbox(e) {
				this.isClause = !this.isClause;
			}
		}
	};
</script>

<style lang="scss">
	page {
		background: #007aff;
		height: 100%;
	}

	.login-wrap {
		height: 100%;

		.top-wrap {
			padding-top: 180rpx;
			height: 420rpx;
		}

		.main-wrap {
			position: fixed;
			padding: 40rpx;
			background: #ffffff;
			border-top-left-radius: 0;
			border-top-right-radius: 0;
			top: 420rpx;
			left: 0;
			right: 0;
			bottom: 0;

			.register-wrap {
				margin-top: 30rpx;
				display: flex;

				.now-btn {
					margin-left: 20rpx;
				}
			}

			.footer-wrap {
				position: fixed;
				bottom: 40rpx;
				left: 0;
				right: 0;
				color: #909399;
				text-align: center;

				.other-login-wrap {
					margin-bottom: 50rpx;
				}

				.copyright {
					font-size: 16rpx;
					margin-top: 20rpx;
				}

				.clause {
					font-size: 28rpx;

					.check_text {
						display: inline-block;
						vertical-align: middle;
					}

					.service,
					.privacy {
						color: #3c9cff;
					}
				}
			}
		}

		.other-wrap {
			.one-click-login-pop-wrap {
				.content-wrap {
					display: flex;
					flex-direction: column;
					align-items: center;
					margin-top: 90rpx;

					.title {
						margin-top: 30rpx;
						font-size: 36rpx;
					}

					.btn-login {
						margin-top: 100rpx;
						margin-bottom: 60rpx;
						width: 320rpx;
						border-radius: 20rpx
					}
				}
			}
		}
	}
</style>
